package com.xcity.game.rank;

import java.util.concurrent.TimeUnit;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.game.common.MessageConst;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.request.GetRankingListOfAreaRequest;
import com.xcity.pomelo.request.GetRankingListOfLvlRequest;
import com.xcity.pomelo.request.GetRankingListRequest;
import com.xcity.pomelo.request.GetRankingRequest;
import com.xcity.pomelo.request.RankingListRequest;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.PlayerRankingResponse;
import com.xcity.pomelo.response.RankingListResponse;

import naga.x.App;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.Service;
import naga.x.service.ServiceException;
import naga.x.util.CommonUtils;

@OPHandler
public class RankingService implements Service {
	
//	protected static final Logger LOG = LoggerFactory.getLogger(RankingService.class);

	@Override
	public void startup() throws ServiceException {
		RankingListEnum[] enums = RankingListEnum.vals;
		for (RankingListEnum e : enums) {
			RankingList instance = e.instance;
			instance.init();
			App.getApp().getEventManager().register(instance);
		}
		App.getApp().getSchedule().scheduleWithFixedDelay(new Runnable() {
			@Override
			public void run() {
				RankingListEnum[] enums = RankingListEnum.vals;
				for (RankingListEnum e : enums) {
					e.instance.doSort();
				}
			}
		}, 0, 30, TimeUnit.SECONDS);
	}

	@Override
	public void shutdown() throws ServiceException {
		RankingListEnum[] enums = RankingListEnum.vals;
		for (RankingListEnum e : enums) {
			e.instance.save();
		}
	}

	@Override
	public String getId() {
		return getClass().getName();
	}
	
	@PomeloOP(PomeloRoute.GET_RANKING)
	protected void requestRanking(Session session, GetRankingRequest req) {
		Player player = session.getClient();
		RankingList rl = RankingListEnum.ASSETS.instance;
		Ranking rank = rl.findEntry(player);
		int index = rank != null ? rank.getIndex() + 1 : rl.getCapacity() + CommonUtils.randomInt(10, 100);
		session.send(new PlayerRankingResponse(req.getSerial(), index));
	}
	
	@PomeloOP(PomeloRoute.GET_AREA_RANKING_LIST)
	protected void requestRankingListOfAreaAssets(Session session, GetRankingListOfAreaRequest req) {
		req.setType(RankingListEnum.ASSETS.instance.getType());
		requestRankingList(session, req);
	}
	
	@PomeloOP(PomeloRoute.GET_ALL_RANKING_LIST)
	protected void requestRankingListOfAssets(Session session, GetRankingListRequest req) {
		req.setType(RankingListEnum.ASSETS.instance.getType());
		requestRankingList(session, req);
	}

	@PomeloOP(PomeloRoute.GET_RANKING_LIST_OF_LEVEL)
	protected void requestRankingListOfLevel(Session session, GetRankingListOfLvlRequest req) {
		req.setType(RankingListEnum.LEVEL.instance.getType());
		requestRankingList(session, req);
	}
	
	@PomeloOP(PomeloRoute.RANKING_LIST)
	protected void requestRankingList(Session session, RankingListRequest req) {
		RankingListEnum rle = RankingListEnum.forType(req.getType());
		if (rle == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.ILLEGAL_OPERATION));
			return;
		}
		Player player = session.getClient();
		RankingList rl = rle.instance;
		Ranking entry = rl.findEntry(player);
		int index = entry != null ? entry.getIndex() + 1 : rl.getCapacity() + CommonUtils.randomInt(10, 100);
		session.send(new RankingListResponse(req.getSerial(), req.getType(), index, rl.getEntries(player), 0, 20));
	}

}
